package com.dreamchaser.familydevice.controller.device;

import com.dreamchaser.familydevice.annotation.IgnoreUrl;
import com.dreamchaser.familydevice.constant.DeviceCache;
import com.dreamchaser.familydevice.constant.ResponseCode;
import com.dreamchaser.familydevice.dao.ComponentMapper;
import com.dreamchaser.familydevice.domain.MessageVO;
import com.dreamchaser.familydevice.domain.dto.DeviceExpression;
import com.dreamchaser.familydevice.util.device.ByteHexUtil;
import com.dreamchaser.familydevice.util.device.DeviceDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 *
 *
 * @author: SJie
 * @Date: 2020/8/9 0:13
 * @description:
 *
 */
@RestController
@Api(value = "设备操作接口", tags = "设备操作接口")
@RequestMapping("deviceOperation")
@Validated
@Slf4j
public class DeviceController {
    @Autowired
    ComponentMapper componentMapper;

    /**
     * 通用设备控制
     *
     * @return
     */
    @IgnoreUrl
    @ApiOperation("设备控制")
    @GetMapping("/{deviceId}/{operation}")
    public MessageVO deviceOperation(@NotNull @PathVariable("deviceId") Integer deviceId,
                                     @NotBlank @PathVariable("operation") @Length(min = 2, max = 2) String operation) {
        int[] data = {ByteHexUtil.hexStrToHex(operation)};
        DeviceDataUtil.send(deviceId, data);
        return new MessageVO().back(ResponseCode.OK, "success");

    }


    /**
     * 通用设备更新
     *
     * @return
     */
    @IgnoreUrl
    @ApiOperation("通用设备更新")
    @GetMapping("/device/update")
    public MessageVO updateDevice() {
        DeviceCache.ComExperssion = componentMapper.selectListExpression();
        return new MessageVO().back(ResponseCode.OK, "success");

    }


}
